clear all

%% PARAMS
SECTOR_ID = 4;


%% Chosing stocks in the same sector and equivalent liquidity
all_sec_id_ok = getLiquidSecurityInSector(SECTOR_ID);

%% Get financial ratios from selected stocks
sub_sec_id_ok = all_sec_id_ok(:,1);
[financial_ratio, dataX] = getFIFromSecurities(sub_sec_id_ok);

%% Normalize data X
dataX_normalize = normalizeDataX(dataX);
%%
[mat_X, vec_y, TotalData, dataX_normalize,pricevec] = preparePriceData(dataX_normalize);
[b,~,~,inmodel] = stepwisefit(mat_X,vec_y);
b=b';
YFIT = b(inmodel)*mat_X(:,inmodel)';
% YFIT=maniArray(YFIT,dataX_normalize(:,end));
%%
Testchunk=chunk(dataX_normalize(:,3),dataX_normalize(:,1));
window=0;
mat_x=[];
mat_y=[];
pos1=1;
n = length(Testchunk);
for i=1:length(Testchunk)-1
    fprintf('Processing %d / %d\n',i, n)
    pos2=sum(dataX_normalize(1:Testchunk(i+1)-1,end));
    [xvariable yvariable]=PrepareRegressX(TotalData(pos1+...
        (isnan(TotalData(pos1,1))):pos2,:),YFIT(Testchunk(i):Testchunk(i+1)-1),...
        dataX_normalize(Testchunk(i):Testchunk(i+1)-1,end));
    pos1=pos2+1;
    mat_x=[mat_x;xvariable];
    mat_y=[mat_y;yvariable];
    %     window=[window;[size(xvariable,1) length(yvariable)]];
end